package org.cytoscape.myapp.cytoscape_app_v1.internal;

import java.util.Collection;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.group.CyGroup;
import org.cytoscape.group.events.GroupAboutToBeRemovedEvent;
import org.cytoscape.group.events.GroupAboutToBeRemovedListener;
import org.cytoscape.group.events.GroupAddedEvent;
import org.cytoscape.group.events.GroupAddedListener;
import org.cytoscape.group.events.GroupCollapsedEvent;
import org.cytoscape.group.events.GroupCollapsedListener;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.events.AddedNodesEvent;
import org.cytoscape.model.events.AddedNodesListener;
import org.cytoscape.model.events.RemovedNodesEvent;
import org.cytoscape.model.events.RemovedNodesListener;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsEvent;
import org.cytoscape.view.model.events.AboutToRemoveNodeViewsListener;
import org.cytoscape.view.presentation.property.BasicVisualLexicon;

public class ViewUtil implements GroupCollapsedListener, AboutToRemoveNodeViewsListener, GroupAboutToBeRemovedListener{
	
	private CyApplicationManager applicationManager;
	private CyNetworkViewManager networkViewManager;

	public ViewUtil(CyApplicationManager applicationManager2,
			CyNetworkViewManager networkViewManager2) {
		// TODO Auto-generated constructor stub
		
		applicationManager = applicationManager2;
		networkViewManager = networkViewManager2;
	}

	public void UpdateView(CyNetwork network){
		Collection<CyNetworkView> views = networkViewManager.getNetworkViews(network);
		for(CyNetworkView view:views)
			view.updateView();
		System.out.println("actualizo vista");
	}

	@Override
	public void handleEvent(GroupCollapsedEvent e) {
		//if (!e.collapsed())
		//{	//applicationManager.getCurrentNetworkView().getNodeView(e.getSource().getGroupNode()).setVisualProperty(BasicVisualLexicon.NODE_VISIBLE, false);
			//e.getSource().expand(e.getNetwork());
			applicationManager.getCurrentNetworkView().updateView();
		//	CyNode padre = e.getSource().getGroupNode();
			/*try{
			
			applicationManager.getCurrentNetworkView().updateView();
			System.out.println("no hay error");
			}
			catch (Exception e1){
				e1.printStackTrace();
				System.out.println("error node visuvle");
			}*/
			
		//}
		///e.getNetwork();
		//System.out.println("handle event collapsed");
		//this.UpdateView(e.getNetwork());
	}

	@Override
	public void handleEvent(GroupAboutToBeRemovedEvent e) {
		// TODO Auto-generated method stub
		System.out.println("Se elimino grupo"+e.getSource().toString());
		applicationManager.getCurrentNetworkView().updateView();
	}

	@Override
	public void handleEvent(AboutToRemoveNodeViewsEvent e) {
		// TODO Auto-generated method stub
		e.getSource().updateView();
		
	}
	
}
